package com.example.hzq.zqb.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.example.hzq.zqb.R;
import com.example.hzq.zqb.adapter.GrandAdapter;
import com.example.hzq.zqb.adapter.NetworkImageHolderView;
import com.example.hzq.zqb.bean.GrandBean;
import com.example.hzq.zqb.bean.GrandgoodsBean;
import com.example.hzq.zqb.model.Model;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.example.hzq.zqb.fragment.Fragment1.imageurl;

/**
 * Created by hzq on 2017/7/13.
 */

public class GrandActivity extends AppCompatActivity{
    private ImageButton back;
    private int grandid;
    private GrandBean grandBean;
    private ConvenientBanner convenientBanner;
    private List<String> networkImages= new ArrayList<String>();
    private ImageLoader imageLoader;
    private TextView top;
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager manager;
    private GrandAdapter grandAdapter;
    private int page,lastVisibleItemPosition;
    private List<GrandgoodsBean>list=null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grand);
        page=1;
        init();
        netinit();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    private void init(){
        grandBean=(GrandBean)getIntent().getSerializableExtra("grand");
        convenientBanner=(ConvenientBanner)findViewById(R.id.grandconven);
        back=(ImageButton)findViewById(R.id.back1);
        top=(TextView)findViewById(R.id.toptext);
        top.setText(grandBean.getName());
        recyclerView=(RecyclerView)findViewById(R.id.grandrecycle);
        manager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(manager);
        recyclerView.setHasFixedSize(true);
        grandAdapter=new GrandAdapter(GrandActivity.this);
        grandAdapter.setList(list);
        grandid=Integer.valueOf(grandBean.getId());
        Model model=new Model();
        Call<List<GrandgoodsBean>>call=model.goodsList(page,grandid);
        call.enqueue(new Callback<List<GrandgoodsBean>>() {
            @Override
            public void onResponse(Call<List<GrandgoodsBean>> call, Response<List<GrandgoodsBean>> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    list=response.body();
                    grandAdapter.setList(list);
                }
               else  Toast.makeText(GrandActivity.this,"获取资源失败", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(Call<List<GrandgoodsBean>> call, Throwable t) {
                Toast.makeText(GrandActivity.this,"404.grandactivity", Toast.LENGTH_SHORT).show();
            }
        });
       /* recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == list.size()) {
                    page+=1;
                    Model model=new Model();
                    Call<List<GrandgoodsBean>> call=model.goodsList(page,grandid);
                    call.enqueue(new Callback<List<GrandgoodsBean>>() {
                        @Override
                        public void onResponse(Call<List<GrandgoodsBean>> call, Response<List<GrandgoodsBean>> response) {
                            if(response.isSuccessful()&&response.body()!=null)
                            {
                                list.addAll(response.body());
                                grandAdapter.setList(list);
                            }else{
                                Toast.makeText(GrandActivity.this,"没有更多了", Toast.LENGTH_SHORT).show();
                            }
                        }
                        @Override
                        public void onFailure(Call<List<GrandgoodsBean>> call, Throwable t) {
                            Toast.makeText(GrandActivity.this,"请检查网络连接",Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItemPosition=list.size()-1;
            }
        });*/
        recyclerView.setAdapter(grandAdapter);
    }

    private void netinit() {
        imageLoader=ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(this));
        for(int i=0;i<grandBean.getImages().size();i++){
            networkImages.add(imageurl+grandBean.getImages().get(i));
        }
        convenientBanner.setPages(new CBViewHolderCreator<NetworkImageHolderView>() {
            @Override
            public NetworkImageHolderView createHolder() {
                return new NetworkImageHolderView();
            }
        }, networkImages).setPageIndicator(new int[]{R.drawable.ic_page_indicator, R.drawable.ic_page_indicator_focused})
                .startTurning(2000);

    }
}
